var gulp = require('gulp'),
      Proxy = require('gulp-connect-proxy');
    connect = require('gulp-connect'),
    minifycss = require('gulp-minify-css'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    del = require('del'),
    obfuscate = require('gulp-obfuscate');
gulp.task('server', function() {
      // connect.server({ root: 'app', livereload: true })
     connect.server({
        root: "project",
        port: 8000,
        livereload: true,
        middleware: function (connect, opt) {
            opt.route = '/proxy';
            var proxy = new Proxy(opt);
            return [proxy];
        }
    });

})
gulp.task('html', function() {
     gulp.src('ph/html/*.html').pipe(connect.reload())
})
gulp.task('watch', function() {
    gulp.watch(['ph/html/*.html'], ['html'])
})
//压缩css
gulp.task('minifycss', function() {
    return gulp.src('project/ph/css/*.css')
          .pipe(minifycss())    //压缩的文件
          .pipe(gulp.dest('project/ph/css'));  //输出文件夹
});

//压缩js
gulp.task('minifyjs', function() {
    return gulp.src('project/ph/js/*.js')
        // .pipe(concat('main.js'))    //合并所有js到main.js
        // .pipe(gulp.dest('minified/js'))    //输出main.js到文件夹
        .pipe(rename({suffix: '.min'}))   //rename压缩后的文件名
        .pipe(uglify()) //压缩
        // .pipe(obfuscate())//混淆
        .pipe(gulp.dest('project/ph/minjs'));  //输出


});


//执行压缩前，先删除文件夹里的内容
gulp.task('clean', function(cb) {
    del(['project/ph/css', 'project/ph/minjs'], cb)
});
gulp.task('default', ['watch','server'], function() {
    gulp.start('minifycss', 'minifyjs');
});
// gulp.task('default', ['watch', 'server'])
